package com.oa.controller;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.oa.pojo.EmpInfo;
import com.oa.pojo.Employee;
import com.oa.pojo.User;
import com.oa.service.EmpInfoService;
import com.oa.service.EmployeeService;
import com.oa.service.UserService;
import com.oa.utils.MD5Utils;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/emp/*")
public class EmpInfoServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String uri = request.getRequestURI();
        String methodName = uri.substring(uri.lastIndexOf("/") + 1);
        if (methodName.equals("list")) {
            this.list(request, response);
        } else if (methodName.equals("add")) {
            this.add(request, response);
        } else if (methodName.equals("del")) {
            this.del(request, response);
        } else if (methodName.equals("edit")) {
            this.edit(request, response);
        }
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String dept = request.getParameter("dept");
        String empName = request.getParameter("empName");
        String level = request.getParameter("level");
        String title = request.getParameter("title");
        String employeeId = request.getParameter("employeeId");
        EmployeeService employeeService = new EmployeeService();

        Employee employee = new Employee();
        employee.setEmployeeId(Long.valueOf(employeeId));
        employee.setLevel(Integer.parseInt(level));
        employee.setTitle(title);
        employee.setName(empName);
        employee.setDepartmentId(Long.valueOf(dept));

        employeeService.update(employee);

        Map<String,Object> result = new HashMap<>();
        // layui 规定的数据表格返回格式
        result.put("code", "0");
        result.put("msg","");
        result.put("count", 1);
        result.put("data", "");
        String jsonString = JSON.toJSONString(result);
        response.getWriter().println(jsonString);


    }

    private void del(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String employeeId = request.getParameter("employeeId");
        EmployeeService employeeService = new EmployeeService();
        System.out.println("-"+employeeId);
        employeeService.deleteById(Long.parseLong(employeeId));
        System.out.println("---"+employeeId);
        Map<String,Object> result = new HashMap<>();
        // layui 规定的数据表格返回格式
        result.put("code", "0");
        result.put("msg","");
        result.put("count", 1);
        result.put("data", "");
        String jsonString = JSON.toJSONString(result);
        response.getWriter().println(jsonString);
    }

    private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String dept = request.getParameter("dept");
        String empName = request.getParameter("empName");
        String level = request.getParameter("level");
        String password = request.getParameter("password");
        String salt = request.getParameter("salt");
        String title = request.getParameter("title");
        String username = request.getParameter("username");
        System.out.println(dept + "--" + empName);

        EmployeeService employeeService = new EmployeeService();
        Employee employee = new Employee();
        employee.setName(empName);
        employee.setDepartmentId(Long.parseLong(dept));
        employee.setTitle(title);
        employee.setLevel(Integer.parseInt(level));
        employeeService.insert(employee);
        System.out.println("----->" + employee.getEmployeeId());

        UserService userService = new UserService();
        User user = new User();
        user.setEmployeeId(employee.getEmployeeId());
        user.setUsername(username);
        user.setSalt(Integer.parseInt(salt));
        user.setPassword(MD5Utils.md5Digest(password, Integer.parseInt(salt)));
        userService.insert(user);

        Map<String,Object> result = new HashMap<>();
        // layui 规定的数据表格返回格式
        result.put("code", "0");
        result.put("msg","");
        result.put("count", 1);
        result.put("data", "");

        String jsonString = JSON.toJSONString(result);
        response.getWriter().println(jsonString);
    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        EmpInfoService empInfoService = new EmpInfoService();
        List<EmpInfo> empInfoList = empInfoService.selectAll();
        Map<String,Object> result = new HashMap<>();
        // layui 规定的数据表格返回格式
        result.put("code", "0");
        result.put("msg","");
        result.put("count", empInfoList.size());
        result.put("data", empInfoList);

        String jsonString = JSON.toJSONString(result);
        response.getWriter().println(jsonString);
    }
}
